<template>
	<view class="activityPage">
		<!-- 活动页面 -->
		<u-navbar title="活动" :safeAreaInsetTop="true">
			<view class="u-nav-slot" slot="left"></view>
		</u-navbar>
		<view class="activity"></view>
		<view class="activityDiv">
			<h1>Today</h1>
			<span>{{timeData[1]-0}}月{{timeData[2]-0}}日 {{timeData[3]}}</span>
		</view>
		<view class="activityDivCalendar">
			<Calendar @selected-change="datechange" @swiperChange="swiperChange"></Calendar>
		</view>
		<view class="activityDivToday">
			<h1>
				当日活动
				<span>{{swiperChangeMonth}}</span>
			</h1>
		</view>
		<view class="activityDivTabs">
			<u-tabs :current="current" :list="tabsList" lineWidth="30" lineColor="#FF6633" :activeStyle="{
			            color: '#303133',
			            fontWeight: 'bold',
			            transform: 'scale(1.05)'
			        }" :inactiveStyle="{
			            color: '#606266',
			            transform: 'scale(1)'
			        }" itemStyle="padding-left: 15px; padding-right: 15px; height: 34px;" @change="tabsChange">
			</u-tabs>
		</view>
		<view class="activityDivList">
			<view class="activityDivItem" v-for="(item,index) in activityListData" :key="index"
				@click="goDetsils(item)">
				<view class="activityDivItemImg">
					<img :src="item.photo[0]" alt="" />
				</view>
				<view class="activityDivItemCon">
					<view class="activityDivItemConName">
						{{item.name}}
					</view>
					<view class="activityDivItemContime">
						{{item.activityTime.join('至')}}
					</view>
					<view class="activityDivItemConaddress">
						{{item.address}}
					</view>
					<view class="activityDivItemConstatus">
						<u-tag :text="item.status" plain size="mini" type="warning"></u-tag>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		activityType,
		activityList
	} from '@/api/home.js'
	import Calendar from "@/components/calendar/calendar.vue"
	export default {
		components: {
			Calendar
		},
		data() {
			return {
				// 当前时间
				timeDataToday: '',
				// 当前日期
				timeData: [],
				// 月份变化
				swiperChangeMonth: '',
				tabsList: [], //菜单列表
				current: 0, //菜单切换
				type: '',
				pageNum: 1, // 当前页码
				pageSize: 10, // 每页条数
				total: 0, // 总数据量
				isLoading: false, // 是否正在加载
				showLoading: false, // 显示加载中
				showNoMore: false, // 显示没有更多
				activityListData: [],
			};
		},
		onLoad() {
			this.timeData = this.$utils.formatDate()
			this.timeDataToday = `${this.timeData[0]}-${this.timeData[1]}-${this.timeData[2]}`
			this.getTabsList()
		},
		// 页面滚动到底部
		onReachBottom() {
			// 页面滚动到底部时触发
			if (this.activityListData.length < this.total) {
				this.pageSize += 10;
				this.getList();
			} else if (this.activityListData.length >= this.total) {
				this.showNoMore = true;
				this.showLoading = false;
			}
		},
		methods: {
			// 点击去详情
			goDetsils(item) {
				uni.navigateTo({
					url: `/pages/activityDetails/activityDetails?id=${item.id}`
				})
			},
			// 获取列表数据
			async getList() {
				// 显示加载状态
				this.isLoading = true;
				this.showLoading = true;
				this.showNoMore = false;
				let par = {
					time: this.timeDataToday,
					type: this.type != '全部' ? this.type : '',
					pageNum: this.pageNum,
					pageSize: this.pageSize
				}
				let res = await activityList(par)
				if (res.code == 200) {
					const newData = res.data.list;
					this.total = res.data.total;
					// 合并数据
					if (this.pageNum == 1) {
						this.activityListData = newData;
					} else {
						this.activityListData = this.activityListData.concat(newData);
					}
					// 控制显示状态
					if (this.activityListData.length >= this.total) {
						this.showNoMore = true;
						this.showLoading = false;
					} else {
						this.showNoMore = false;
						this.showLoading = true;
					}
				} else {
					this.isLoading = false;
					this.showLoading = false;
				}
			},
			// 菜单切换
			tabsChange(e) {
				this.type = e.name
				this.pageSize = 10
				this.getList()
			},
			// 获取菜单
			async getTabsList() {
				let res = await activityType()
				if (res.code == 200) {
					let arr = []
					res.data.forEach(str => {
						arr.push({
							name: str
						})
					})
					arr.unshift({
						name: '全部'
					})
					this.tabsList = arr
					this.type = this.tabsList[0].name
					this.getList()
				}
			},
			// 当日活动右侧的时间
			swiperChange(e) {
				this.swiperChangeMonth = e
			},
			// 点击时间
			datechange(e) {
				this.timeDataToday = e.fullDate
				this.pageSize = 10
				this.getList()
			}
		}
	}
</script>

<style lang="scss">
	.activityPage {
		.activityDivList {
			margin-top: 20rpx;
			display: flex;
			width: 100%;
			flex-direction: column;
			gap: 20rpx;
			padding: 0 30rpx;

			.activityDivItem {
				border-radius: 20rpx;
				padding: 20rpx;
				display: flex;
				background-color: #ffffff;
				gap: 20rpx;

				.activityDivItemCon {
					width: 75%;
					overflow-wrap: break-word;

					.activityDivItemConstatus {
						margin-top: 10rpx;
						max-width: 145rpx;
					}

					.activityDivItemConaddress {
						font-size: 26rpx;
						color: #666666;
					}

					.activityDivItemContime {
						font-size: 26rpx;
						color: #666666;
						margin: 10rpx 0;
					}

					.activityDivItemConName {
						font-size: 38rpx;
						font-weight: bold;
					}
				}

				.activityDivItemImg {
					min-height: 200rpx;

					img {
						width: 140rpx;
						height: 100%;
						border-radius: 20rpx;
					}
				}
			}
		}

		.activityDivTabs {
			margin-top: 20rpx;
		}

		.activityDivToday {
			margin-top: 20rpx;
			padding: 0 30rpx;

			h1 {
				font-size: 40rpx;
				font-weight: bold;

				span {
					font-size: 24rpx;
					font-weight: normal;
					margin-left: 10rpx;
				}
			}
		}

		.activityDivCalendar {
			width: 100%;
		}

		.activityDiv {
			display: flex;
			align-items: flex-end;
			padding: 0 30rpx;
			gap: 10rpx;
			margin-bottom: 20rpx;

			span {
				font-size: 24rpx;
			}

			h1 {
				font-size: 40rpx;
			}
		}

		.activity {
			width: 100%;
			background: linear-gradient(#F1E1E6, #CFD9E9, #F5F8FB);
			padding-top: 170rpx;
		}

		::v-deep .u-status-bar,
		::v-deep .u-navbar__content {
			background: none !important;
		}
	}

	page {
		background-color: #F5F8FB;
	}
</style>